﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sausage.Web.Code;
using Sausage.Common;
using Sausage.Web.Domain;
using Sausage.Web.Models;

namespace Sausage.Web.Controllers
{
    /// <summary>
    /// 材料数据字典控制器
    /// </summary>
    /// <remarks></remarks>
    public class MaterialController : Controller
    {
        /// <summary>
        /// 默认动作。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 查询工程材料列表
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>查询的工程项目信息列表</returns>
        /// <remarks></remarks>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Select()
        {
            var data = MaterialHelper.QueryMaterialTree();
            return Json(new JsonResultViewModel { success = true, message = "工程材料信息加载成功！", data = data },
                        JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 检索指定编号的工程材料。
        /// </summary>
        /// <param name="id">工程材料的编号。</param>
        /// <returns>工程材料信息。</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Details(Guid id)
        {
            var material = MaterialHelper.GetMaterialByID(id);
            if (material == null)
                return Json(new JsonResultViewModel { success = false, message = "查找不到此工程材料信息！", data = null },
                            JsonRequestBehavior.AllowGet);

            return Json(new JsonResultViewModel
            {
                success = true,
                data = new TreeNodeData()
                {
                    id = material.MaterialID,
                    parentID = material.ParentID,
                    name = material.MaterialName,
                    pinyin = material.MaterialPinYin,
                    unit = material.Unit,
                    leaf = material.IsLeaf
                }
            },
            JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 删除材料
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Delete(TreeNodeData data)
        {
            var material = MaterialHelper.GetMaterialByID(data.id);

            if (material == null)
                return Json(new JsonResultViewModel { success = false, message = "没有此工程材料信息！", data = null },
                            JsonRequestBehavior.AllowGet);
            if (!material.IsLeaf)
                return Json(new JsonResultViewModel { success = false, message = "此工程材料含有子工程材料信息，不可删除，请先删除其包含的工程材料信息！", data = null },
                            JsonRequestBehavior.AllowGet);


            if (MaterialHelper.DeleteMaterial(data.id) == DeleteResult.UsingError)
            {
                return Json(new JsonResultViewModel { success = false, message = "工程材料删除失败，其它数据正在使用此工程材料！" }, JsonRequestBehavior.AllowGet);
            }

            //返回结果
            return Json(new JsonResultViewModel { success = true, message = "工程材料删除成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 更新工程材料
        /// </summary>
        /// <param name="data">要更新的工程材料信息。</param>
        /// <returns></returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Update(TreeNodeData data)
        {
            var material = MaterialHelper.GetMaterialByName(data.name);
            if (material != null && material.MaterialID != data.id)
                return Json(new JsonResultViewModel { success = false, message = "已有同名工程材料信息，请更换材料名称！", data = null },
                            JsonRequestBehavior.AllowGet);

            MaterialHelper.UpdateMaterial(data);

            //返回结果
            return Json(new JsonResultViewModel { success = true, message = "更新工程材料信息成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 创建工程材料
        /// </summary>
        /// <returns>执行结果。</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Create(TreeNodeData data)
        {
            var material = MaterialHelper.GetMaterialByName(data.name);
            if (material != null)
                return Json(new JsonResultViewModel { success = false, message = "已有同名工程材料信息，请更换材料名称！", data = null },
                            JsonRequestBehavior.AllowGet);

            MaterialHelper.CreateMaterial(data);

            //返回结果
            return Json(new JsonResultViewModel { success = true, message = "添加工程材料信息成功！" }, JsonRequestBehavior.AllowGet);
        }
    }
}
